//
//  BIDLifeViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/18.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDLifeViewController.h"
#import "BIDStoreGoodsListViewController.h"
#import "BIDStoreCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

static NSString *storeListURL = @"memberAndroid/list.action";

@interface BIDLifeViewController ()<BIDBaseListViewDelegate>
{
    BOOL _isRegister;
}
@end

@implementation BIDLifeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商铺";
    self.navigationController.navigationBarHidden = NO;
    [self loadData];
}

- (void)didMoveToParentViewController:(UIViewController *)parent
{
    parent.title = @"生活";
    parent.navigationItem.rightBarButtonItem = nil;
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], storeListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"searchParam.userType=2&page=%ld&rows=%ld", self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
        }
        [self.myTableView reloadData];
        [self autoDecideToShowEmptyView];
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDStoreCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    NSDictionary *dic = self.dataSourceArr[row];
    BIDStoreCell *storeCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSString *strFilePath = dic[@"photo"];
    if(strFilePath)
    {
        strFilePath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], strFilePath];
        [storeCell.imgView setImageWithURL:[NSURL URLWithString:strFilePath] placeholderImage:[UIImage imageNamed:PIC_PLACEHOLDER]];
    }
    else
    {
        [storeCell.imgView setImage:[UIImage imageNamed:PIC_PLACEHOLDER]];
    }
    storeCell.nameLabel.text = dic[@"trueName"];
    cell = storeCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    BIDStoreGoodsListViewController *vc = [[BIDStoreGoodsListViewController alloc] init];
    vc.title = dic[@"trueName"];
    vc.storeID = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 104.f;
}

@end
